package com.rs.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rs.dao.ZhClassesMapper;
import com.rs.dao.ZhUserMapper;
import com.rs.model.CommonResult;
import com.rs.model.ZhClasses;
import com.rs.model.ZhUser;
import com.rs.service.ZhClassesService;

@Service
@Transactional
public class ZhClassesServiceImpl implements ZhClassesService {
	
	@Autowired
	ZhClassesMapper zhClassesMapper;
	
	@Autowired
	ZhUserMapper zhUserMapper;
	
	//将日期格式转换
	SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
	
	static final Integer NUMBER= 10;

	@Override
	public Page<ZhUser> selecClassStudents(Map<String,Integer> map) {
		// TODO Auto-generated method stub
		PageHelper.startPage(map.get("pageNum"),NUMBER);
		Page<ZhUser> page = zhUserMapper.selecClassStudents(map.get("uCaid"),map.get("uPoid"));
		return page;
	}

	@Override
	public CommonResult<Integer> deleteByPrimaryKey(Integer clId) {
		// TODO Auto-generated method stub
		int state = zhClassesMapper.deleteByPrimaryKey(clId);
		if(state==0){
			return new CommonResult<Integer>(200,"删除失败",state);
		}
		return new CommonResult<Integer>(200,"删除成功",state);
	}

	@Override
	public CommonResult<Integer> insertSelective(ZhClasses record) {
		// TODO Auto-generated method stub
		//添加前判断班级名字是否也存在
		int classCount = zhClassesMapper.selectZhClassesNotNameRepeat(record.getClName());
		if(classCount>0){
			return new CommonResult<Integer>(200,"班级名字重复");
		}
		try {
			if(record.getClTimeString() !=null){
				record.setClTime(sm.parse(record.getClTimeString()));
			}
			if(record.getClEndtimeString() !=null){
				record.setClEndtime(sm.parse(record.getClEndtimeString()));
			}
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		int state = zhClassesMapper.insertSelective(record);
		if(state==0){
			return new CommonResult<Integer>(200,"添加失败",state);
		}
		return new CommonResult<Integer>(200,"添加成功",state);
	}

	@Override
	public CommonResult<Integer> updateByPrimaryKeySelective(ZhClasses record) {
		// TODO Auto-generated method stub
		//修改前判断班级名字是否也存在
		int classCount = zhClassesMapper.selectZhClassesNotNameRepeat(record.getClName());
		if(classCount>0){
			return new CommonResult<Integer>(200,"班级名字重复",0);
		}
		int state = zhClassesMapper.updateByPrimaryKeySelective(record);
		if(state==0){
			return new CommonResult<Integer>(200,"修改失败",state);
		}
		return new CommonResult<Integer>(200,"修改成功",state);
	}

	@Override
	public Page<ZhClasses> graduationZhClassess(Integer pageNum) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum,NUMBER);
		Page<ZhClasses> page = zhClassesMapper.graduationZhClassess();
		for(ZhClasses zh:page.getResult()){
			if(zh.getClTime() !=null){
			zh.setClTimeString(sm.format(zh.getClTime()));	
			}
			if(zh.getClEndtime() !=null){
				zh.setClEndtimeString(sm.format(zh.getClEndtime()));	
			}
		}
		return page;
	}

	@Override
	public Page<ZhClasses> notGraduateZhClassess(Integer pageNum) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum,NUMBER);
		Page<ZhClasses> page = zhClassesMapper.notGraduateZhClassess();
		for(ZhClasses zh:page.getResult()){
			if(zh.getClTime() !=null){
			zh.setClTimeString(sm.format(zh.getClTime()));	
			}
			if(zh.getClEndtime() !=null){
				zh.setClEndtimeString(sm.format(zh.getClEndtime()));	
			}
		}
		return page;
	}

	@Override
	public CommonResult<Integer> updateGraduationToApplyFor(Integer clId) {
		// TODO Auto-generated method stub
		int status = zhClassesMapper.updateGraduationToApplyFor(clId);
		if(status==0){
			return new CommonResult<Integer>(200,"修改失败");
		}
		return new CommonResult<Integer>(200,"修改成功");
	}

	@Override
	public CommonResult<Integer> updateGraduationAudit(ZhClasses record) {
		// TODO Auto-generated method stub
		int status = zhClassesMapper.updateGraduationAudit(record);
		if(status==0){
			return new CommonResult<Integer>(200,"修改失败");
		}
		return new CommonResult<Integer>(200,"修改成功");
	}

	@Override
	public ZhClasses selectByPrimaryKey(Integer clId) {
		// TODO Auto-generated method stub
		return zhClassesMapper.selectByPrimaryKey(clId);
	}

	@Override
	public List<ZhClasses> listZhClasses() {
		// TODO Auto-generated method stub
		return zhClassesMapper.listZhClasses();
	}

	@Override
	public Page<ZhClasses> graduationZhClassessByEnd(ZhClasses record) {
		PageHelper.startPage(record.getPageNum(),NUMBER);
		return zhClassesMapper.graduationZhClassessByEnd(record);
	}

}
